<?php
namespace app\home\controller;

use app\home\common\Common;
use app\home\common\Page;
use app\home\common\Pagetwo;
use app\home\service\Smstmp;
use app\infrastructure\uri\Request;
use app\infrastructure\uri\Uri;
use think\Db;

/**
 * Created by PhpStorm.
 * User: YD
 * Date: 2017/4/26
 * Time: 16:52
 */
class Analyse extends Common
{
    private $client;
    public  function  __construct(){
        parent::__construct();
        $this->client=new Request();
    }

    public function daily(){
        $date=date("Y-m-d",strtotime("-1 day"));
        $loanData=Db::table("analyse_loan_new")
            ->where("create_time",$date)
            ->find();
        if(!empty($loanData)){
            $loanSubmitMore=json_decode($loanData['loan_submit_group_user'],true);
            $loanData['loanSubmitUserCount']=count($loanSubmitMore);
            $loanSuccessMore=json_decode($loanData['loan_success_group_user'],true);
            $loanData['loanSuccessUserCount']=count($loanSuccessMore);
            $loanMore=0;
            foreach($loanSuccessMore as $k=>$v){
               if($v >= 2){
                   $loanMore++;
               }
             }
            $loanData['totalMoreUserCount']=$loanMore;
        }
        $userData=Db::table("analyse_user_new")
            ->where("create_time",$date)
            ->find();
        $repayData=Db::table("analyse_repay_new")
            ->where("create_time",$date)
            ->find();

        $getAppflyerUrl=Uri::PHPRESTHOST.Uri::GETAPPFLYER.$date;
        $appflyerData=$this->client->getArrayData($getAppflyerUrl);
        $this->assign("userData",$userData);
        $this->assign("loanData",$loanData);
        $this->assign("repayData",$repayData);
        $this->assign("appflyerData",$appflyerData);
        return view();
    }

    /**
     * 新日报统计
     */
//    public function daily()
//    {
//        $where = [];
//        $today = date('Y-m-d');
//        if (!empty($_GET['date'])) {
//            $today=$_GET['date'];
//            $startDate = strtotime($_GET['date']);
//            $endDate = $startDate + 3600 * 24;
//            $where['create_time'][] = ['EGT', $startDate];
//            $where['create_time'][] = ['ELT', $endDate];
//
//        }
//
//        $yesDayData=$this->yesdayDaily($today);
////        dd($yesDayData);die;
//
//        $today = strtotime($today);
//
//        //查询appFlayers上的数据
//       $from_date=date('Y-m-d', strtotime('-2 day'));
//        $appsData=Db::table('analyse_appsflayer')
//            ->where('from_date',$from_date)
//            ->find();
//        if(!empty($appsData)){
//            $appDate=$appsData['to_date'];
//            $appsFlayerData=$appsData['content'];
//            $totalappsFlayerData=$appsData['totalData'];
//
//            $aa=str_replace("\n",',',$appsFlayerData);
//            $totalappsFlayerData=str_replace("\n",',',$totalappsFlayerData);
//            $newtotalappsFlayerData=explode(',',$totalappsFlayerData);
//            $totalAppInstall=0;
//            foreach($newtotalappsFlayerData as $k=>$v){
//                $intCur=floor($k/14)*14+8;
//                if($intCur <14){
//                    $intCur=-1;
//                }
//                if(($k+1) == $intCur ){
//                    $totalAppInstall += $v;
//                }
//            }
//            $newAppsFlayersData=explode(',',$aa);
//            $moreAppIndtall=0;
//            foreach($newAppsFlayersData as $k=>$v){
//               if($v == $appDate){
//                   $moreAppIndtall += $newAppsFlayersData[$k+7];
//               }
//            }
//        }else{
//            $moreAppIndtall=0;
//            $totalAppInstall=0;
//        }
//
//
//        $data = Db::table('analyse_data')
//            ->where($where)
//            ->order("create_time desc")->find();
//        if (empty($data)) {
//            $data = [];
//        } else {
//            //借款数据（已放款）
//            $dataLoan = json_decode($data['loan_total_success_user']);
//            $yesloanSuccessMoney = 0;
//            $dataloanSuccessMoney = 0;
//            foreach ($dataLoan as $k => $v) {
//                $dataloanSuccessMoney = $dataloanSuccessMoney + $v->actualAmount;
//            }
//            $loanSuccessCount = count($dataLoan);
//
//            //借款用户
//            $newYesLoanUser = [];
//            $dataLoanUser = json_decode($data['loan_total_success_groupby_user']);
//            $newDataLoanUser = [];
//            $loanMoreTwo = [];
//            foreach ($dataLoanUser as $k => $v) {
//                $newDataLoanUser[] = [
//                    'userPhone' => $k,
//                    'loanCount' => $v
//                ];
//                if ($v > 1) {
//                    $loanMoreTwo[] = [
//                        'userPhone' => $k,
//                        'loanCount' => $v
//                    ];
//                }
//            }
//            //新增借款用户
//            $moreLoanSuccessUser = count($newDataLoanUser) - count($newYesLoanUser);
//
//            //借款申请数据
//            $dataLoanSubmit = json_decode($data['loan_total_user']);
//            $dataLoanSubmitCount = count($dataLoanSubmit);
//
//            //借款申请用户
//            $newYesLoanSubmitUser = [];
//            $dataLoanSubmitUser = json_decode($data['loan_total_groupby_user']);
//            $newDataLoanSubmitUser = [];
//            foreach ($dataLoanSubmitUser as $k => $v) {
//                $newDataLoanSubmitUser[] = [
//                    'userPhone' => $k,
//                    'loanCount' => $v
//                ];
//            }
//
//            //借款用户总量
//            $loan_total = count($newDataLoanUser);
//            //借款申请用户总量
//            $loan_submit_total = count($newDataLoanSubmitUser);
//
//
//
//            //短信审核数据
//            //通过
//            $dataSmsPass = json_decode($data['sms_pass_user']);
//            //不通过
//            $dataSmsNoPass = json_decode($data['sms_nopass_user']);
//            //无短信
//            $dataSmsNohave = json_decode($data['sms_nohave_user']);
//            //未查看
//            $dataSmsNoReview = json_decode($data['sms_noreview_user']);
//
//            //新增短信审核通过
//            $SmsPass = count($dataSmsPass);
//            //新增短信审核不通过
//            $SmsNoPass = count($dataSmsNoPass);
//            //新增无短信
//            $SmsNoHave = count($dataSmsNohave);
//            //新增未查看
//            $SmsNoReview = count($dataSmsNoReview);
//
//            //产品数据统计
//            $dataProduct37 = json_decode($data['product_37_loan']);
//            $dataProduct315 = json_decode($data['product_314_loan']);
//            $dataProduct67 = json_decode($data['product_67_loan']);
//            $dataProduct615 = json_decode($data['product_614_loan']);
//
//            //3000 7
//            $dataproduct37count = count($dataProduct37);
//            $dataproduct37money = 0;
//            foreach ($dataProduct37 as $k => $v) {
//                $dataproduct37money = $dataproduct37money + $v->actualAmount;
//            }
//            //3000 15
//            $dataproduct315count = count($dataProduct315);
//            $dataproduct315money = 0;
//            foreach ($dataProduct315 as $k => $v) {
//                $dataproduct315money = $dataproduct315money + $v->actualAmount;
//            }
//            //6000 7
//            $dataproduct67count = count($dataProduct67);
//            $dataproduct67money = 0;
//            foreach ($dataProduct67 as $k => $v) {
//                $dataproduct67money = $dataproduct67money + $v->actualAmount;
//            }
//            //6000 15
//            $dataproduct615count = count($dataProduct615);
//            $dataproduct615money = 0;
//            foreach ($dataProduct615 as $k => $v) {
//                $dataproduct615money = $dataproduct615money + $v->actualAmount;
//            }
//
//            //还款数据统计
//            $dataRepay = json_decode($data['repay_total_user']);
//            $dataRepayCount = count($dataRepay);
//            $yesRepayMoney = 0;
//            $dataRepayMoney = 0;
//            foreach ($dataRepay as $k => $v) {
//                $dataRepayMoney = $dataRepayMoney + $v->repay_money;
//            }
//            $moreRepayMoney = $dataRepayMoney - $yesRepayMoney;
//
//            //坏账数据总计
//            $badMoney = json_decode($data['badmoney_user']);
//            $badMoneyCount = 0;
//            if(!empty($badMoney)){
//                foreach ($badMoney as $k => $v) {
//                    $badMoneyCount = $badMoneyCount + $v->totalRepay + $v->duefee;
//                }
//            }
//
//            $badMoneyToday = json_decode($data['badmoney_today_user']);
//            $badMoneyTodayCount = 0;
//            if(!empty($badMoneyToday)){
//                foreach ($badMoneyToday as $k => $v) {
//                    $badMoneyTodayCount = $badMoneyTodayCount + $v->totalRepay + $v->duefee;
//                }
//            }
//
//            //逾期数据统计
//            $dueMoney = json_decode($data['due_user']);
//            $dueMoneyCount = 0;
//            if(!empty($dueMoney)){
//                foreach ($dueMoney as $k => $v) {
//                    $dueMoneyCount = $dueMoneyCount + $v->totalRepay + $v->duefee;
//                }
//            }
//
//            //今日预计逾期
//            $dueMoneyToday = json_decode($data['due_today_user']);
//            $dueMoneyTodayCount = 0;
//            if(!empty($dueMoneyToday)){
//                foreach ($dueMoneyToday as $k => $v) {
//                    $dueMoneyTodayCount = $dueMoneyTodayCount + $v->totalRepay + $v->duefee;
//                }
//            }
//
//            //续借数据统计
//            $loanMoreTwoCount = count($loanMoreTwo);
//
//            $newData=$data;
//            $data = [
//                "totalRegister" => $newData['user_total'],//总注册量
//                "moreUser" => $newData['user_total']-$yesDayData['totalRegister'],                 //新增注册量
//                "loan_total" => $loan_total,            //借款成功用户总量
//                "loan_submit_total" => $loan_submit_total,//借款申请用户总量
//
//                "moreLoanSuccessUser" => $loan_total-$yesDayData['loan_total'],//新增借款用户总量
//                "more_loanSubmit" => $loan_submit_total-$yesDayData['loan_submit_total'],//新增借款申请用户总量
//
//                "moreSmsPass" => $SmsPass-$yesDayData['SmsPass'],             //新增短信审核通过总量
//                "moreSmsNoPass" => $SmsNoPass-$yesDayData['SmsNoPass'],        //新增短信审核不通过总量
//                "moreSmsNoHave" => $SmsNoHave-$yesDayData['SmsNoHave'],        //新增无短信总量
//                "moreSmsNoReview" => $SmsNoReview-$yesDayData['SmsNoReview'],       //新增短信未查看总量
//
//                "dataproduct37money" => $dataproduct37money,       //3000 7 借款金额
//                "dataproduct37count" => $dataproduct37count,       //3000 7 借款笔数
//                "dataproduct315money" => $dataproduct315money,       //3000 15 借款金额
//                "dataproduct315count" => $dataproduct315count,       //3000 15 借款笔数
//                "dataproduct615money" => $dataproduct615money,       //6000 15 借款金额
//                "dataproduct615count" => $dataproduct615count,       //6000 15 借款笔数
//                "dataproduct67money" => $dataproduct67money,       //6000 7 借款金额
//                "dataproduct67count" => $dataproduct67count,       //6000 7 借款笔数
//
//                "dataLoanSubmitCount" => $dataLoanSubmitCount,       //总借款申请数
//                "loanSuccessCount" => $loanSuccessCount,            //总借款成功数
//                "dataloanSuccessMoney" => $dataloanSuccessMoney,       //总借款金额
//
//                "moreLoanSuccessMoney" => $dataloanSuccessMoney-$yesDayData['dataloanSuccessMoney'],       //新增借款金额
//                "moreLoanSubmit" => $dataLoanSubmitCount - $yesDayData['dataLoanSubmitCount'],       //新增借款申请数
//
//                "moreLoanSuccess" => $loanSuccessCount - $yesDayData['loanSuccessCount'],       //新增借款成功数
//
//                "dataRepayMoney" => $dataRepayMoney / 100,       //还款总金额
//                "dataRepayCount" => $dataRepayCount,       //还款总笔数
//                "moreRepayMoney" => ($dataRepayMoney/100) - $yesDayData['dataRepayMoney'],       //新增还款金额
//                "moreRepayCount" =>$dataRepayCount-$yesDayData['dataRepayCount'],       //新增还款笔数
//
//
//                "dueMoneyCount" => $dueMoneyCount,       //逾期总金额
//                "dueMoneyTodayCount" => $dueMoneyTodayCount,       //预计今日逾期总金额
//
//                "badMoneyCount" => $badMoneyCount,       //坏账总金额
//                "badMoneyToday" => $badMoneyTodayCount,       //预计今日坏账总金额
//
//                "loanMoreTwoCount" => $loanMoreTwoCount,       //续借总量
//                "moreLanMoreTwoCount" =>$loanMoreTwoCount-$yesDayData['loanMoreTwoCount'],       //新增续借总量
//
//                "moreAppInstall"=>$moreAppIndtall,       //新增下载量
//                "totalAppInstall"=>$totalAppInstall,    //app总下载量
//
//                'create_time' => $newData['create_time']
//            ];
//            if (isset($_GET['extend']) && $_GET['extend'] == 1) {
//                $column_name = [
//                    [
//                        '注册用户总量',
//                        '借款用户总量',
//                        '借款申请用户总量',
//                        '借款笔数',
//                        '还款笔数',
//                        '续借用户总量',
//                        'APP下载总量',
//                        '总借款金额',
//                        '总还款金额',
//                        '总逾期金额',
//                        '总坏账金额',
//                        '新增注册用户',
//                        '新增借款用户',
//                        '新增借款申请用户',
//                        '新增借款笔数',
//                        '新增还款笔数',
//                        '新增续借用户',
//                        'APP新增下载量',
//                        '新增借款金额',
//                        '新增还款金额',
//                        '新增逾期金额',
//                        '新增坏账金额'
//                    ]
//                ];
//                $extendData[] = [
//                    "totalRegister" => $newData['user_total'],
//                    "totalLoanUser" => $loan_total,
//                    "totalSubmitLoanUser" => $loan_submit_total,
//                    "loanSuccessCount" => $loanSuccessCount,
//                    "totalRepayUser" => $dataRepayCount,
//                    "totalLoanMoreUser" => $loanMoreTwoCount,
//                    "apptotaldownload" => 888,
//                    "totalLoanMoney" => $dataloanSuccessMoney,
//                    "totaRepayMoney" => $dataRepayMoney / 100,
//                    "totalDue" => $dueMoneyCount,
//                    "totalBadMoney" => $badMoneyCount,
//                    "moreUserToday" => $moreUser,
//                    "totalLoanToday" => $moreLoanSuccessUser,
//                    "totalLoanSummitToday" => $more_loanSubmit,
//                    "totalLoanToday22" => $moreLoanSuccess,
//                    "totalRepayUserToday" => $moreRepayCount,
//                    "totalLoanMoreUserToday" => $moreLanMoreTwoCount,
//                    "appNewUser" => 888,
//                    "addLoanMoney" => $moreLoanSuccessMoney,
//                    "addRepayMoney" => $moreRepayMoney / 100,
//                    "addDueMoney" => $dueMoneyTodayCount,
//                    "addBadMoney" => $badMoneyTodayCount
//                ];
//                $data = array_merge($column_name, $extendData);
//                $this->extendToExcel($data);
//            }
//            $this->assign('data', $data);
//            return view();
//        }
//    }

    /**
     * 查找昨日数据
     */
    private function yesdayDaily($date){
        $toDay=strtotime($date);
        $yesDay=$toDay - 24*3600;
        $where['create_time'][] = ['EGT', $yesDay];
        $where['create_time'][] = ['ELT', $toDay];
        $data = Db::table('analyse_data')
            ->where($where)
            ->order("create_time desc")->find();
        if (empty($data)) {
            $data = [];
        } else {
            //借款数据（已放款）
            $dataLoan = json_decode($data['loan_total_success_user']);
            $dataloanSuccessMoney = 0;
            foreach ($dataLoan as $k => $v) {
                $dataloanSuccessMoney = $dataloanSuccessMoney + $v->actualAmount;
            }
            $loanSuccessCount = count($dataLoan);

            //借款用户
            $newYesLoanUser = [];
            $dataLoanUser = json_decode($data['loan_total_success_groupby_user']);
            $newDataLoanUser = [];
            $loanMoreTwo = [];
            foreach ($dataLoanUser as $k => $v) {
                $newDataLoanUser[] = [
                    'userPhone' => $k,
                    'loanCount' => $v
                ];
                if ($v > 1) {
                    $loanMoreTwo[] = [
                        'userPhone' => $k,
                        'loanCount' => $v
                    ];
                }
            }
            //新增借款用户
            $moreLoanSuccessUser = count($newDataLoanUser) - count($newYesLoanUser);

            //借款申请数据
            $dataLoanSubmit = json_decode($data['loan_total_user']);
            $dataLoanSubmitCount = count($dataLoanSubmit);

            //借款申请用户
            $newYesLoanSubmitUser = [];
            $dataLoanSubmitUser = json_decode($data['loan_total_groupby_user']);
            $newDataLoanSubmitUser = [];
            foreach ($dataLoanSubmitUser as $k => $v) {
                $newDataLoanSubmitUser[] = [
                    'userPhone' => $k,
                    'loanCount' => $v
                ];
            }

            //借款用户总量
            $loan_total = count($newDataLoanUser);
            //借款申请用户总量
            $loan_submit_total = count($newDataLoanSubmitUser);

            //短信审核数据
            //通过
            $dataSmsPass = json_decode($data['sms_pass_user']);
            //不通过
            $dataSmsNoPass = json_decode($data['sms_nopass_user']);
            //无短信
            $dataSmsNohave = json_decode($data['sms_nohave_user']);
            //未查看
            $dataSmsNoReview = json_decode($data['sms_noreview_user']);

            //产品数据统计
            $dataProduct37 = json_decode($data['product_37_loan']);
            $dataProduct315 = json_decode($data['product_314_loan']);
            $dataProduct67 = json_decode($data['product_67_loan']);
            $dataProduct615 = json_decode($data['product_614_loan']);

            //3000 7
            $dataproduct37count = count($dataProduct37);
            $dataproduct37money = 0;
            foreach ($dataProduct37 as $k => $v) {
                $dataproduct37money = $dataproduct37money + $v->actualAmount;
            }
            //3000 15
            $dataproduct315count = count($dataProduct315);
            $dataproduct315money = 0;
            foreach ($dataProduct315 as $k => $v) {
                $dataproduct315money = $dataproduct315money + $v->actualAmount;
            }
            //6000 7
            $dataproduct67count = count($dataProduct67);
            $dataproduct67money = 0;
            foreach ($dataProduct67 as $k => $v) {
                $dataproduct67money = $dataproduct67money + $v->actualAmount;
            }
            //6000 15
            $dataproduct615count = count($dataProduct615);
            $dataproduct615money = 0;
            foreach ($dataProduct615 as $k => $v) {
                $dataproduct615money = $dataproduct615money + $v->actualAmount;
            }

            //还款数据统计
            $dataRepay = json_decode($data['repay_total_user']);
            $dataRepayCount = count($dataRepay);
            $yesRepayMoney = 0;
            $dataRepayMoney = 0;
            foreach ($dataRepay as $k => $v) {
                $dataRepayMoney = $dataRepayMoney + $v->repay_money;
            }
            $moreRepayMoney = $dataRepayMoney - $yesRepayMoney;

            //坏账数据总计
            $badMoney = json_decode($data['badmoney_user']);
            $badMoneyCount = 0;
            if(!empty($badMoney)){
                foreach ($badMoney as $k => $v) {
                    $badMoneyCount = $badMoneyCount + $v->totalRepay + $v->duefee;
                }
            }
            $badMoneyToday = json_decode($data['badmoney_today_user']);
            $badMoneyTodayCount = 0;
            if(!empty($badMoneyToday)){
                foreach ($badMoneyToday as $k => $v) {
                    $badMoneyTodayCount = $badMoneyTodayCount + $v->totalRepay + $v->duefee;
                }
            }
            //逾期数据统计
            $dueMoney = json_decode($data['due_user']);
            $dueMoneyCount = 0;
            if(!empty($dueMoney)){
                foreach ($dueMoney as $k => $v) {
                    $dueMoneyCount = $dueMoneyCount + $v->totalRepay + $v->duefee;
                }
            }
            //今日预计逾期
            $dueMoneyToday = json_decode($data['due_today_user']);
            $dueMoneyTodayCount = 0;
            if(!empty($dueMoneyToday)){
                foreach ($dueMoneyToday as $k => $v) {
                    $dueMoneyTodayCount = $dueMoneyTodayCount + $v->totalRepay + $v->duefee;
                }
            }
            //续借数据统计
            $loanMoreTwoCount = count($loanMoreTwo);
            //新增续借用户
            $newData=$data;
            $data = [
                "totalRegister" => $newData['user_total'],//总注册量
                "loan_total" => $loan_total,            //借款成功用户总量
                "loan_submit_total" => $loan_submit_total,//借款申请用户总量

                "dataproduct37money" => $dataproduct37money,       //3000 7 借款金额
                "dataproduct37count" => $dataproduct37count,       //3000 7 借款笔数
                "dataproduct315money" => $dataproduct315money,       //3000 15 借款金额
                "dataproduct315count" => $dataproduct315count,       //3000 15 借款笔数
                "dataproduct615money" => $dataproduct615money,       //6000 15 借款金额
                "dataproduct615count" => $dataproduct615count,       //6000 15 借款笔数
                "dataproduct67money" => $dataproduct67money,       //6000 7 借款金额
                "dataproduct67count" => $dataproduct67count,       //6000 7 借款笔数

                "dataLoanSubmitCount" => $dataLoanSubmitCount,       //总借款申请数
                "loanSuccessCount" => $loanSuccessCount,            //总借款成功数
                "dataloanSuccessMoney" => $dataloanSuccessMoney,       //总借款金额

                "dataRepayMoney" => $dataRepayMoney / 100,       //还款总金额
                "dataRepayCount" => $dataRepayCount,       //还款总笔数

                "SmsPass" => count($dataSmsPass),                //短信审核通过总量
                "SmsNoPass" => count($dataSmsNoPass),           //短信审核不通过总量
                "SmsNoHave" => count($dataSmsNohave),           //无短信总量
                "SmsNoReview" => count($dataSmsNoReview),       //短信未查看总量

                "dueMoneyCount" => $dueMoneyCount,       //逾期总金额
                "dueMoneyTodayCount" => $dueMoneyTodayCount,       //预计今日逾期总金额

                "badMoneyCount" => $badMoneyCount,       //坏账总金额
                "badMoneyToday" => $badMoneyTodayCount,       //预计今日坏账总金额

                "loanMoreTwoCount" => $loanMoreTwoCount,       //续借总量
                'create_time' => $newData['create_time']
            ];
        }
        return $data;
    }



    /**
     * 日用户增长量分析
     */
    public function userIncreateMent(){
        $where=[];
        if(isset($_GET['date']) && !empty($_GET['date'])){
            $date=$_GET['date'];
            $nowMonth=$date."-01";
            $nextMonth=strtotime($nowMonth) + 24*3600*30;
            $nextMonth=date('Y-m-d',$nextMonth);
            $where['create_time'][] = ['EGT', $nowMonth];
            $where['create_time'][] = ['ELT', $nextMonth];
        }
        $data=Db::table("analyse_user")
            ->where('name','day')
            ->where($where)
            ->order("create_time DESC")
            ->find();
//        dd(Db::table("analyse_user")->getLastSql());
        $this->assign('data',$data);
        return view();
    }

    /**
     * 还款分析
     */
    public function loanRepay(){
        $where=[];
        if (!empty($_GET['date'])) {
            $startDate = strtotime($_GET['date']);
            $endDate = $startDate + 3600 * 24;
            $where['create_time'][] = ['EGT', $startDate];
            $where['create_time'][] = ['ELT', $endDate];
        }
        $todayRepayData= Db::table("analyse_repay")
            ->where($where)
            ->order("create_time","DESC")
            ->find();
        $repayData=json_decode($todayRepayData['repay_data']);
        $dueData=json_decode($todayRepayData['due_data']);
        $create_time=$todayRepayData['create_time'];
        $totalNeedRepayMoney=0;  //待还款总额
        $totalNeedRepayCount=0;  //待还款总笔数

        $totalRepayMoney=0;  //总计还款金额
        $totalUserLoan=0;    //还款总笔数

        $beforeRepayMoney=0; //提前还款金额
        $beforeRepayCount=0; //提前还款笔数

        $onTimeRepayMoney=0; //按时还款金额
        $onTimeRepayCount=0; //按时还款笔数

        $dueOneToThreeRepayMoney=0;  //逾期1到3天还款金额
        $dueOneToThreeRepayCount=0;  //逾期1到3天还款笔数
        $dueFourToSevenRepayMoney=0;  //逾期4到7天还款金额
        $dueFourToSevenRepayCount=0;  //逾期4到7天还款笔数
        $dueEightToFifteenRepayMoney=0;  //逾期8到15天还款金额
        $dueEightToFifteenRepayCount=0;  //逾期8到15天还款笔数
        $dueSixteenToThrityRepayMoney=0;  //逾期16天到30还款金额
        $dueSixteenToThrityRepayCount=0;  //逾期16天到30还款笔数
        $dueMoreThrityRepayMoney=0;  //逾期30以上还款金额
        $dieMoreThrityRepayCount=0;  //逾期30以上还款笔数

        $totalDueMoney=0;        //总逾期金额
        $totalDueCount=0;       //总逾期笔数
        $dueOneToThreeMoney=0; //逾期1到3天金额
        $dueOneToThreeCount=0; //逾期1到3天笔数
        $dueFourToSevenMoney=0; //逾期4到7天金额
        $dueFourToSevenCount=0; //逾期4到7天笔数
        $dueEightToFifteenMoney=0; //逾期8到15天金额
        $dueEightToFifteenCount=0; //逾期8到15天笔数
        $dueSixteenToThrityMoney=0; //逾期16到30天金额
        $dueSixteenToThrityCount=0; //逾期16到30天笔数

        $moreExpireMoney=0;   //昨日新增到期总额,还款日为前一日的金额本息和
        $moreExoireCount=0;   //昨日新增到期笔数,还款日为前一日的借款笔数

        $totalExpireMoney=0;     //到期总金额
        $totalExpireCount=0;     //到期总笔数
         if(!empty($todayRepayData)){
        foreach ($repayData as $k=>$v) {
           $totalRepayMoney += $v->repay_money;
            $totalUserLoan ++;
            $repayTime=$v->repay_time;
            $arrayedTime=floor(($v->loanArrangedTime)/1000);
            $realRepayTime=date("Y-m-d",$repayTime);
            $realRepayTime=strtotime($realRepayTime);
            $repayPeriod=$v->loanRepayPeriod;
            $hopeRepayTime=$arrayedTime+$repayPeriod*24*3600-24*3600;
            $hopeRepayTime=date("Y-m-d",$hopeRepayTime);
            $hopeRepayTime=strtotime($hopeRepayTime);
            if($realRepayTime == $hopeRepayTime){
                //按时还款
                $onTimeRepayMoney += $v->repay_money;
                $onTimeRepayCount ++;
            }
            else if($realRepayTime > $hopeRepayTime){
                 //逾期还款
                $diffTime=$realRepayTime - $hopeRepayTime;
                $diffDay=$diffTime/(24*3600);
                if($diffDay >= 3 && $diffDay <= 5){
                    $dueOneToThreeRepayMoney += $v->repay_money;
                    $dueOneToThreeRepayCount ++;
                }else if($diffDay >= 4 && $diffDay <= 7){
                    $dueFourToSevenRepayMoney += $v->repay_money;
                    $dueFourToSevenRepayCount ++;
                }else if($diffDay >= 8 && $diffDay <= 15){
                    $dueEightToFifteenRepayMoney += $v->repay_money;
                    $dueEightToFifteenRepayCount ++;
                }else if($diffDay >= 16 && $diffDay <=30){
                    $dueSixteenToThrityRepayMoney += $v->repay_money;
                    $dueSixteenToThrityRepayCount ++;
                }else if($diffDay > 30){
                    $dueMoreThrityRepayMoney += $v->repay_money;
                    $dieMoreThrityRepayCount ++;
                }
            }
            else if($realRepayTime < $hopeRepayTime){
                //提前还款
                $beforeRepayMoney += $v->repay_money;
                $beforeRepayCount ++;
            }
        }

        //逾期信息分析
        foreach ($dueData as $kk=>$vv) {
            $totalNeedRepayMoney += $vv->realRepayMoney;
            $totalNeedRepayCount ++;
            //到期总额,到期总笔数
             $dueDay=$vv->dueDay;
            //逾期数据分析
            if($dueDay > 0 ){
                $totalDueMoney += $vv->realRepayMoney;
                $totalDueCount ++;
                if($dueDay >= 1 && $dueDay <= 3){
                    $dueOneToThreeMoney += $vv->realRepayMoney;
                    $dueOneToThreeCount ++;
                }else if($dueDay >= 4 && $dueDay <= 7){
                    $dueFourToSevenMoney += $vv->realRepayMoney;
                    $dueFourToSevenCount ++;
                }else if($dueDay >= 8 && $dueDay <= 15){
                    $dueEightToFifteenMoney += $vv->realRepayMoney;
                    $dueEightToFifteenCount ++;
                }else if($dueDay >= 16 && $dueDay <= 30){
                    $dueSixteenToThrityMoney += $vv->realRepayMoney;
                    $dueSixteenToThrityCount ++;
                }
            }
            if($dueDay == -1){
                $moreExpireMoney += $vv->realRepayMoney;
                $moreExoireCount ++;
            }
        }
         }
        $data=[
            "totalNeedRepayMoney"=>$totalNeedRepayMoney,  //待还款总额
            "totalNeedRepayCount"=>$totalNeedRepayCount,  //待还款总笔数

            "totalRepayMoney"=>$totalRepayMoney/100,  //总计还款金额
            "totalUserLoan"=>$totalUserLoan,    //还款总笔数

            "beforeRepayMoney"=>$beforeRepayMoney/100, //提前还款金额
            "beforeRepayCount"=>$beforeRepayCount, //提前还款笔数

            "onTimeRepayMoney"=>$onTimeRepayMoney/100, //按时还款金额
            "onTimeRepayCount"=>$onTimeRepayCount, //按时还款笔数

            "dueOneToThreeRepayMoney"=>$dueOneToThreeRepayMoney/100,  //逾期1到3天还款金额
            "dueOneToThreeRepayCount"=>$dueOneToThreeRepayCount,//逾期1到3天还款笔数
            "dueFourToSevenRepayMoney"=>$dueFourToSevenRepayMoney/100,  //逾期4到7天还款金额
            "dueFourToSevenRepayCount"=>$dueFourToSevenRepayCount,  //逾期4到7天还款笔数
            "dueEightToFifteenRepayMoney"=>$dueEightToFifteenRepayMoney/100,  //逾期8到15天还款金额
            "dueEightToFifteenRepayCount"=>$dueEightToFifteenRepayCount,//逾期8到15天还款笔数
            "dueSixteenToThrityRepayMoney"=>$dueSixteenToThrityRepayMoney/100, //逾期16天到30还款金额
            "dueSixteenToThrityRepayCount"=>$dueSixteenToThrityRepayCount,  //逾期16天到30还款笔数
            "dueMoreThrityRepayMoney"=>$dueMoreThrityRepayMoney/100,  //逾期30以上还款金额
            "dieMoreThrityRepayCount"=>$dieMoreThrityRepayCount,  //逾期30以上还款笔数

            "totalDueMoney"=>$totalDueMoney,        //总逾期金额
           "totalDueCount"=>$totalDueCount,       //总逾期笔数
            "dueOneToThreeMoney"=>$dueOneToThreeMoney, //逾期1到3天金额
            "dueOneToThreeCount"=>$dueOneToThreeCount, //逾期1到3天笔数
            "dueFourToSevenMoney"=>$dueFourToSevenMoney, //逾期4到7天金额
            "dueFourToSevenCount"=>$dueFourToSevenCount, //逾期4到7天笔数
            "dueEightToFifteenMoney"=>$dueEightToFifteenMoney, //逾期8到15天金额
            "dueEightToFifteenCount"=>$dueEightToFifteenCount, //逾期8到15天笔数
            "dueSixteenToThrityMoney"=>$dueSixteenToThrityMoney,//逾期16到30天金额
            "dueSixteenToThrityCount"=>$dueSixteenToThrityCount, //逾期16到30天笔数

            "moreExpireMoney"=>$moreExpireMoney,   //昨日新增到期总额,还款日为前一日的金额本息和
            "moreExoireCount"=>$moreExoireCount,  //昨日新增到期笔数,还款日为前一日的借款笔数

            "totalExpireMoney"=>$moreExpireMoney+$totalDueMoney+($totalRepayMoney-$beforeRepayMoney-$onTimeRepayMoney),     //到期总金额
            "totalExpireCount"=>$totalDueCount+$moreExoireCount+($totalUserLoan-$beforeRepayCount-$onTimeRepayCount),     //到期总笔数
            "create_time"=>$create_time
        ];

         $this->assign('data',$data);
         return view();
    }


    /**
     * 逾期分析
     */
    public function dueLoanAnalyse(){
        $where=[];
        if (!empty($_GET['date'])) {
            $startDate = strtotime($_GET['date']);
            $endDate = $startDate + 3600 * 24;
            $where['create_time'][] = ['EGT', $startDate];
            $where['create_time'][] = ['ELT', $endDate];
        }
        $today = date('Y-m-d');
        $today = strtotime($today);
        $yesDate = date('Y-m-d', strtotime('-1 day'));
        $yesDate = strtotime($yesDate);
        $yesTodayWhere['create_time'][] = ['EGT', $yesDate];
        $yesTodayWhere['create_time'][] = ['ELT', $today];

        $yesData=$this->dueLoanAnalyseYesday($yesTodayWhere);
        $todayRepayData= Db::table("analyse_repay")
            ->where($where)
            ->order("create_time","DESC")
            ->find();
        $repayData=json_decode($todayRepayData['repay_data']);
        $dueData=json_decode($todayRepayData['due_data']);
        $create_time=$todayRepayData['create_time'];
        $totalNeedRepayMoney=0;  //待还款总额
        $totalNeedRepayCount=0;  //待还款总笔数

        $totalRepayMoney=0;  //总计还款金额
        $totalUserLoan=0;    //还款总笔数

        $beforeRepayMoney=0; //提前还款金额
        $beforeRepayCount=0; //提前还款笔数

        $onTimeRepayMoney=0; //按时还款金额
        $onTimeRepayCount=0; //按时还款笔数

        $dueOneToThreeRepayMoney=0;  //逾期1到3天还款金额
        $dueOneToThreeRepayCount=0;  //逾期1到3天还款笔数
        $dueFourToSevenRepayMoney=0;  //逾期4到7天还款金额
        $dueFourToSevenRepayCount=0;  //逾期4到7天还款笔数
        $dueEightToFifteenRepayMoney=0;  //逾期8到15天还款金额
        $dueEightToFifteenRepayCount=0;  //逾期8到15天还款笔数
        $dueSixteenToThrityRepayMoney=0;  //逾期16天到30还款金额
        $dueSixteenToThrityRepayCount=0;  //逾期16天到30还款笔数
        $dueMoreThrityRepayMoney=0;  //逾期30以上还款金额
        $dieMoreThrityRepayCount=0;  //逾期30以上还款笔数

        $totalDueMoney=0;        //总逾期金额
        $totalDueCount=0;       //总逾期笔数
        $dueOneToThreeMoney=0; //逾期1到3天金额
        $dueOneToThreeCount=0; //逾期1到3天笔数
        $dueFourToSevenMoney=0; //逾期4到7天金额
        $dueFourToSevenCount=0; //逾期4到7天笔数
        $dueEightToFifteenMoney=0; //逾期8到15天金额
        $dueEightToFifteenCount=0; //逾期8到15天笔数
        $dueSixteenToThrityMoney=0; //逾期16到30天金额
        $dueSixteenToThrityCount=0; //逾期16到30天笔数

        $moreExpireMoney=0;   //昨日新增到期总额,还款日为前一日的金额本息和
        $moreExoireCount=0;   //昨日新增到期笔数,还款日为前一日的借款笔数

        $totalExpireMoney=0;     //到期总金额
        $totalExpireCount=0;     //到期总笔数

        $badDueMoney=0;     //坏账金额
        $badDueCount=0;     //坏账笔数

        $totalBadDueMoneyRate=0;    //总坏账率（金额）
        $totalBadDueCountRate=0;    //总坏账率（笔数）
        if(!empty($todayRepayData)){
        foreach ($repayData as $k=>$v) {
            $totalRepayMoney += $v->repay_money;
            $totalUserLoan ++;
            $repayTime=$v->repay_time;
            $arrayedTime=floor(($v->loanArrangedTime)/1000);
            $realRepayTime=date("Y-m-d",$repayTime);
            $realRepayTime=strtotime($realRepayTime);
            $repayPeriod=$v->loanRepayPeriod;
            $hopeRepayTime=$arrayedTime+$repayPeriod*24*3600-24*3600;
            $hopeRepayTime=date("Y-m-d",$hopeRepayTime);
            $hopeRepayTime=strtotime($hopeRepayTime);
            if($realRepayTime == $hopeRepayTime){
                //按时还款
                $onTimeRepayMoney += $v->repay_money;
                $onTimeRepayCount ++;
            }
            else if($realRepayTime > $hopeRepayTime){
                //逾期还款
                $diffTime=$realRepayTime - $hopeRepayTime;
                $diffDay=$diffTime/(24*3600);
                if($diffDay >= 3 && $diffDay <= 5){
                    $dueOneToThreeRepayMoney += $v->repay_money;
                    $dueOneToThreeRepayCount ++;
                }else if($diffDay >= 4 && $diffDay <= 7){
                    $dueFourToSevenRepayMoney += $v->repay_money;
                    $dueFourToSevenRepayCount ++;
                }else if($diffDay >= 8 && $diffDay <= 15){
                    $dueEightToFifteenRepayMoney += $v->repay_money;
                    $dueEightToFifteenRepayCount ++;
                }else if($diffDay >= 16 && $diffDay <=30){
                    $dueSixteenToThrityRepayMoney += $v->repay_money;
                    $dueSixteenToThrityRepayCount ++;
                }else if($diffDay > 30){
                    $dueMoreThrityRepayMoney += $v->repay_money;
                    $dieMoreThrityRepayCount ++;
                }
            }
            else if($realRepayTime < $hopeRepayTime){
                //提前还款
                $beforeRepayMoney += $v->repay_money;
                $beforeRepayCount ++;
            }
        }
        //逾期信息分析
        foreach ($dueData as $kk=>$vv) {
            $totalNeedRepayMoney += $vv->realRepayMoney;
            $totalNeedRepayCount ++;
            //到期总额,到期总笔数
            $dueDay=$vv->dueDay;
            //逾期数据分析
            if($dueDay > 0 ){
                $totalDueMoney += $vv->realRepayMoney;
                $totalDueCount ++;
                if($dueDay >= 1 && $dueDay <= 3){
                    $dueOneToThreeMoney += $vv->realRepayMoney;
                    $dueOneToThreeCount ++;
                }else if($dueDay >= 4 && $dueDay <= 7){
                    $dueFourToSevenMoney += $vv->realRepayMoney;
                    $dueFourToSevenCount ++;
                }else if($dueDay >= 8 && $dueDay <= 15){
                    $dueEightToFifteenMoney += $vv->realRepayMoney;
                    $dueEightToFifteenCount ++;
                }else if($dueDay >= 16 && $dueDay <= 30){
                    $dueSixteenToThrityMoney += $vv->realRepayMoney;
                    $dueSixteenToThrityCount ++;
                }else if($dueDay >30){
                    $badDueMoney += $vv->realRepayMoney;
                    $badDueCount ++;
                }
            }
            if($dueDay == -1){
                $moreExpireMoney += $vv->realRepayMoney;
                $moreExoireCount ++;
            }
        }
        $totalBadDueMoneyRate=$badDueMoney/($moreExpireMoney+$totalDueMoney+($totalRepayMoney-$beforeRepayMoney-$onTimeRepayMoney)/100);
        $totalBadDueCountRate=$badDueCount/($moreExoireCount+($totalUserLoan-$beforeRepayCount-$onTimeRepayCount));

        }
        $data=[
            "totalNeedRepayMoney"=>$totalNeedRepayMoney,  //待还款总额
            "totalNeedRepayCount"=>$totalNeedRepayCount,  //待还款总笔数

            "totalRepayMoney"=>$totalRepayMoney/100,  //总计还款金额
            "totalUserLoan"=>$totalUserLoan,    //还款总笔数

            "beforeRepayMoney"=>$beforeRepayMoney/100, //提前还款金额
            "beforeRepayCount"=>$beforeRepayCount, //提前还款笔数

            "onTimeRepayMoney"=>$onTimeRepayMoney/100, //按时还款金额
            "onTimeRepayCount"=>$onTimeRepayCount, //按时还款笔数

            "dueOneToThreeRepayMoney"=>$dueOneToThreeRepayMoney/100,  //逾期1到3天还款金额
            "dueOneToThreeRepayCount"=>$dueOneToThreeRepayCount,//逾期1到3天还款笔数
            "dueFourToSevenRepayMoney"=>$dueFourToSevenRepayMoney/100,  //逾期4到7天还款金额
            "dueFourToSevenRepayCount"=>$dueFourToSevenRepayCount,  //逾期4到7天还款笔数
            "dueEightToFifteenRepayMoney"=>$dueEightToFifteenRepayMoney/100,  //逾期8到15天还款金额
            "dueEightToFifteenRepayCount"=>$dueEightToFifteenRepayCount,//逾期8到15天还款笔数
            "dueSixteenToThrityRepayMoney"=>$dueSixteenToThrityRepayMoney/100, //逾期16天到30还款金额
            "dueSixteenToThrityRepayCount"=>$dueSixteenToThrityRepayCount,  //逾期16天到30还款笔数
            "dueMoreThrityRepayMoney"=>$dueMoreThrityRepayMoney/100,  //逾期30以上还款金额
            "dieMoreThrityRepayCount"=>$dieMoreThrityRepayCount,  //逾期30以上还款笔数

            "totalDueMoney"=>$totalDueMoney,        //总逾期金额
            "totalDueCount"=>$totalDueCount,       //总逾期笔数
            "dueOneToThreeMoney"=>$dueOneToThreeMoney, //逾期1到3天金额
            "dueOneToThreeCount"=>$dueOneToThreeCount, //逾期1到3天笔数
            "dueFourToSevenMoney"=>$dueFourToSevenMoney, //逾期4到7天金额
            "dueFourToSevenCount"=>$dueFourToSevenCount, //逾期4到7天笔数
            "dueEightToFifteenMoney"=>$dueEightToFifteenMoney, //逾期8到15天金额
            "dueEightToFifteenCount"=>$dueEightToFifteenCount, //逾期8到15天笔数
            "dueSixteenToThrityMoney"=>$dueSixteenToThrityMoney,//逾期16到30天金额
            "dueSixteenToThrityCount"=>$dueSixteenToThrityCount, //逾期16到30天笔数

            "moreExpireMoney"=>$moreExpireMoney,   //昨日新增到期总额,还款日为前一日的金额本息和
            "moreExoireCount"=>$moreExoireCount,  //昨日新增到期笔数,还款日为前一日的借款笔数

            "totalExpireMoney"=>$moreExpireMoney+$totalDueMoney+($totalRepayMoney-$beforeRepayMoney-$onTimeRepayMoney)/100,     //到期总金额
            "totalExpireCount"=>$totalDueCount+$moreExoireCount+($totalUserLoan-$beforeRepayCount-$onTimeRepayCount),     //到期总笔数

            "totalBadDueMoneyRate"=>$totalBadDueMoneyRate,   //总坏账率（金额）
            "totalBadDueCountRate"=>$totalBadDueCountRate,   //总坏账率（笔数）

            "moreBadDueMoney"=>$badDueMoney-$yesData['badDueMoney'],               //昨日新增坏账金额
            "moreBadDueCount"=>$badDueCount-$yesData['badDueCount'],              //昨日新增坏账笔数

            "badDueMoney"=>$badDueMoney,   //坏账总金额
            "badDueCount"=>$badDueCount,   //坏账总笔数

            "create_time"=>$create_time
        ];
        $this->assign('data',$data);
        return view();
    }

    /**
     * 逾期分析前一天数据
     */
    private function dueLoanAnalyseYesday($where){
        $todayRepayData= Db::table("analyse_repay")
            ->where($where)
            ->order("create_time","DESC")
            ->find();
        $repayData=json_decode($todayRepayData['repay_data']);
        $dueData=json_decode($todayRepayData['due_data']);
        $create_time=$todayRepayData['create_time'];
        $totalNeedRepayMoney=0;  //待还款总额
        $totalNeedRepayCount=0;  //待还款总笔数

        $totalRepayMoney=0;  //总计还款金额
        $totalUserLoan=0;    //还款总笔数

        $beforeRepayMoney=0; //提前还款金额
        $beforeRepayCount=0; //提前还款笔数

        $onTimeRepayMoney=0; //按时还款金额
        $onTimeRepayCount=0; //按时还款笔数

        $dueOneToThreeRepayMoney=0;  //逾期1到3天还款金额
        $dueOneToThreeRepayCount=0;  //逾期1到3天还款笔数
        $dueFourToSevenRepayMoney=0;  //逾期4到7天还款金额
        $dueFourToSevenRepayCount=0;  //逾期4到7天还款笔数
        $dueEightToFifteenRepayMoney=0;  //逾期8到15天还款金额
        $dueEightToFifteenRepayCount=0;  //逾期8到15天还款笔数
        $dueSixteenToThrityRepayMoney=0;  //逾期16天到30还款金额
        $dueSixteenToThrityRepayCount=0;  //逾期16天到30还款笔数
        $dueMoreThrityRepayMoney=0;  //逾期30以上还款金额
        $dieMoreThrityRepayCount=0;  //逾期30以上还款笔数

        $totalDueMoney=0;        //总逾期金额
        $totalDueCount=0;       //总逾期笔数
        $dueOneToThreeMoney=0; //逾期1到3天金额
        $dueOneToThreeCount=0; //逾期1到3天笔数
        $dueFourToSevenMoney=0; //逾期4到7天金额
        $dueFourToSevenCount=0; //逾期4到7天笔数
        $dueEightToFifteenMoney=0; //逾期8到15天金额
        $dueEightToFifteenCount=0; //逾期8到15天笔数
        $dueSixteenToThrityMoney=0; //逾期16到30天金额
        $dueSixteenToThrityCount=0; //逾期16到30天笔数

        $moreExpireMoney=0;   //昨日新增到期总额,还款日为前一日的金额本息和
        $moreExoireCount=0;   //昨日新增到期笔数,还款日为前一日的借款笔数

        $totalExpireMoney=0;     //到期总金额
        $totalExpireCount=0;     //到期总笔数

        $badDueMoney=0;     //坏账金额
        $badDueCount=0;     //坏账笔数

        $totalBadDueMoneyRate=0;    //总坏账率（金额）
        $totalBadDueCountRate=0;    //总坏账率（笔数）
         if(!empty($todayRepayData)){
        foreach ($repayData as $k=>$v) {
            $totalRepayMoney += $v->repay_money;
            $totalUserLoan ++;
            $repayTime=$v->repay_time;
            $arrayedTime=floor(($v->loanArrangedTime)/1000);
            $realRepayTime=date("Y-m-d",$repayTime);
            $realRepayTime=strtotime($realRepayTime);
            $repayPeriod=$v->loanRepayPeriod;
            $hopeRepayTime=$arrayedTime+$repayPeriod*24*3600-24*3600;
            $hopeRepayTime=date("Y-m-d",$hopeRepayTime);
            $hopeRepayTime=strtotime($hopeRepayTime);
            if($realRepayTime == $hopeRepayTime){
                //按时还款
                $onTimeRepayMoney += $v->repay_money;
                $onTimeRepayCount ++;
            }
            else if($realRepayTime > $hopeRepayTime){
                //逾期还款
                $diffTime=$realRepayTime - $hopeRepayTime;
                $diffDay=$diffTime/(24*3600);
                if($diffDay >= 3 && $diffDay <= 5){
                    $dueOneToThreeRepayMoney += $v->repay_money;
                    $dueOneToThreeRepayCount ++;
                }else if($diffDay >= 4 && $diffDay <= 7){
                    $dueFourToSevenRepayMoney += $v->repay_money;
                    $dueFourToSevenRepayCount ++;
                }else if($diffDay >= 8 && $diffDay <= 15){
                    $dueEightToFifteenRepayMoney += $v->repay_money;
                    $dueEightToFifteenRepayCount ++;
                }else if($diffDay >= 16 && $diffDay <=30){
                    $dueSixteenToThrityRepayMoney += $v->repay_money;
                    $dueSixteenToThrityRepayCount ++;
                }else if($diffDay > 30){
                    $dueMoreThrityRepayMoney += $v->repay_money;
                    $dieMoreThrityRepayCount ++;
                }
            }
            else if($realRepayTime < $hopeRepayTime){
                //提前还款
                $beforeRepayMoney += $v->repay_money;
                $beforeRepayCount ++;
            }
        }

        //逾期信息分析
        foreach ($dueData as $kk=>$vv) {
            $totalNeedRepayMoney += $vv->realRepayMoney;
            $totalNeedRepayCount ++;
            //到期总额,到期总笔数
            $dueDay=$vv->dueDay;
            //逾期数据分析
            if($dueDay > 0 ){
                $totalDueMoney += $vv->realRepayMoney;
                $totalDueCount ++;
                if($dueDay >= 1 && $dueDay <= 3){
                    $dueOneToThreeMoney += $vv->realRepayMoney;
                    $dueOneToThreeCount ++;
                }else if($dueDay >= 4 && $dueDay <= 7){
                    $dueFourToSevenMoney += $vv->realRepayMoney;
                    $dueFourToSevenCount ++;
                }else if($dueDay >= 8 && $dueDay <= 15){
                    $dueEightToFifteenMoney += $vv->realRepayMoney;
                    $dueEightToFifteenCount ++;
                }else if($dueDay >= 16 && $dueDay <= 30){
                    $dueSixteenToThrityMoney += $vv->realRepayMoney;
                    $dueSixteenToThrityCount ++;
                }else if($dueDay >30){
                    $badDueMoney += $vv->realRepayMoney;
                    $badDueCount ++;
                }
            }
            if($dueDay == -1){
                $moreExpireMoney += $vv->realRepayMoney;
                $moreExoireCount ++;
            }
        }
        $totalBadDueMoneyRate=$totalDueMoney/($moreExpireMoney+$totalDueMoney+($totalRepayMoney-$beforeRepayMoney-$onTimeRepayMoney)/100);
        $totalBadDueCountRate=$totalDueCount/($moreExoireCount+($totalUserLoan-$beforeRepayCount-$onTimeRepayCount));
         }
        $data=[
            "totalNeedRepayMoney"=>$totalNeedRepayMoney,  //待还款总额
            "totalNeedRepayCount"=>$totalNeedRepayCount,  //待还款总笔数

            "totalRepayMoney"=>$totalRepayMoney/100,  //总计还款金额
            "totalUserLoan"=>$totalUserLoan,    //还款总笔数

            "beforeRepayMoney"=>$beforeRepayMoney/100, //提前还款金额
            "beforeRepayCount"=>$beforeRepayCount, //提前还款笔数

            "onTimeRepayMoney"=>$onTimeRepayMoney/100, //按时还款金额
            "onTimeRepayCount"=>$onTimeRepayCount, //按时还款笔数

            "dueOneToThreeRepayMoney"=>$dueOneToThreeRepayMoney/100,  //逾期1到3天还款金额
            "dueOneToThreeRepayCount"=>$dueOneToThreeRepayCount,//逾期1到3天还款笔数
            "dueFourToSevenRepayMoney"=>$dueFourToSevenRepayMoney/100,  //逾期4到7天还款金额
            "dueFourToSevenRepayCount"=>$dueFourToSevenRepayCount,  //逾期4到7天还款笔数
            "dueEightToFifteenRepayMoney"=>$dueEightToFifteenRepayMoney/100,  //逾期8到15天还款金额
            "dueEightToFifteenRepayCount"=>$dueEightToFifteenRepayCount,//逾期8到15天还款笔数
            "dueSixteenToThrityRepayMoney"=>$dueSixteenToThrityRepayMoney/100, //逾期16天到30还款金额
            "dueSixteenToThrityRepayCount"=>$dueSixteenToThrityRepayCount,  //逾期16天到30还款笔数
            "dueMoreThrityRepayMoney"=>$dueMoreThrityRepayMoney/100,  //逾期30以上还款金额
            "dieMoreThrityRepayCount"=>$dieMoreThrityRepayCount,  //逾期30以上还款笔数

            "totalDueMoney"=>$totalDueMoney,        //总逾期金额
            "totalDueCount"=>$totalDueCount,       //总逾期笔数
            "dueOneToThreeMoney"=>$dueOneToThreeMoney, //逾期1到3天金额
            "dueOneToThreeCount"=>$dueOneToThreeCount, //逾期1到3天笔数
            "dueFourToSevenMoney"=>$dueFourToSevenMoney, //逾期4到7天金额
            "dueFourToSevenCount"=>$dueFourToSevenCount, //逾期4到7天笔数
            "dueEightToFifteenMoney"=>$dueEightToFifteenMoney, //逾期8到15天金额
            "dueEightToFifteenCount"=>$dueEightToFifteenCount, //逾期8到15天笔数
            "dueSixteenToThrityMoney"=>$dueSixteenToThrityMoney,//逾期16到30天金额
            "dueSixteenToThrityCount"=>$dueSixteenToThrityCount, //逾期16到30天笔数

            "moreExpireMoney"=>$moreExpireMoney,   //昨日新增到期总额,还款日为前一日的金额本息和
            "moreExoireCount"=>$moreExoireCount,  //昨日新增到期笔数,还款日为前一日的借款笔数

            "totalExpireMoney"=>$moreExpireMoney+$totalDueMoney+($totalRepayMoney-$beforeRepayMoney-$onTimeRepayMoney)/100,     //到期总金额
            "totalExpireCount"=>$totalDueCount+$moreExoireCount+($totalUserLoan-$beforeRepayCount-$onTimeRepayCount),     //到期总笔数

            "totalBadDueMoneyRate"=>$totalBadDueMoneyRate,   //总坏账率（金额）
            "totalBadDueCountRate"=>$totalBadDueCountRate,   //总坏账率（笔数）

            "badDueMoney"=>$badDueMoney,   //坏账总金额
            "badDueCount"=>$badDueCount,   //坏账总笔数

            "create_time"=>$create_time
        ];
        return $data;
    }

    /**
     * 还账分析
     */
    public function badMoneyAnalyse(){
        $todayRepayData= Db::table("analyse_repay")
            ->order("create_time","DESC")
            ->find();
        $dueData=json_decode($todayRepayData['due_data']);
        $create_time=$todayRepayData['create_time'];

         $badDueMoney=0;   //总坏账金额
         $badDueCount=0;   //总坏账笔数
        foreach ($dueData as $kk=>$vv) {
            //到期总额,到期总笔数
            $dueDay=$vv->dueDay;
            //逾期数据分析
            if($dueDay > 30){
                $badDueMoney += $vv->realRepayMoney;
                $badDueCount ++;
            }
        }
        return view();
    }
}